package com.woorigil.social.parser;

import java.io.InputStream;
import java.io.InputStreamReader;

import nu.xom.Element;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.deal.Deal;
import com.woorigil.social.domain.deal.DealListDocument;

public abstract class XmlParser implements Parser {
	@Autowired CategoryMapperFactory categoryMapperFactory;
	
	public DealListDocument parse(InputStream is) throws Exception {
		return parse(new InputStreamReader(is, "UTF-8"));
	}

	protected String getValue(Element element) {
		if (element == null) {
			return null;
		}
		return StringUtils.trim(element.getValue());
	}
	
	protected int findCategoryId(ProviderCode providerCode, Deal deal) {
		try {
			CategoryMapper mapper = categoryMapperFactory.getInstance(providerCode);
			return mapper.getCategoryId(deal);
		} catch (Exception e) {
//			System.out.println(providerCode + " : " + e.getMessage());
			return 0;
		}
	}
}
